package me.uniauto.ctrl;

import me.uniauto.util.BusinessDataCode;
import me.uniauto.util.CommResp;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 异常处理
 * Package:me.uniauto.www.controller
 * Author: Haijian.Sun
 * Date: 2016-09-30 14:13
 * Description:
 */
public abstract class BaseController {
	private Logger logger = LoggerFactory.getLogger(BaseController.class);
	/**
	 * 异常处理
	 */
	@ExceptionHandler(Exception.class)
	public void exceptionHandler(HttpServletRequest request, HttpServletResponse response, Exception ex)
			throws Exception {
		ex.printStackTrace();
		logger.error("Method invoke error", ex);
		//扩展异常处理
		response.setContentType("application/json;charset=UTF-8");
		byte[] bytes = myResp(BusinessDataCode.SERVICE_EXCEPTION).toString().getBytes("UTF-8");

		response.getOutputStream().write(bytes);
	}

	/**
	 * 成功
	 *
	 * @return
	 */
	protected CommResp success() {
		return CommResp.success_resp;
	}

	/**
	 * 含特殊信息的成功，参数会被放到message中而不是data
	 *
	 * @param message
	 * @return
	 */
	protected CommResp success(String message) {
		return new CommResp(BusinessDataCode.SUCCESS.getCode(), message);
	}

	/**
	 * 含数据成功
	 *
	 * @param data
	 * @return
	 */
	protected CommResp success(Object data) {
		CommResp resp = new CommResp(BusinessDataCode.SUCCESS);
		resp.setData(data);
		return resp;
	}

	/**
	 * 含特殊信息和数据成功
	 *
	 * @param message
	 * @param data
	 * @return
	 */
	protected CommResp success(String message, Object data) {
		CommResp resp = new CommResp(BusinessDataCode.SUCCESS.getCode(), message);
		resp.setData(data);
		return resp;
	}

	/**
	 * 失败
	 *
	 * @return
	 */
	protected CommResp fail() {
		return CommResp.fail_resp;
	}

	/**
	 * 含特殊信息失败
	 *
	 * @param message
	 * @return
	 */
	protected CommResp fail(String message) {
		return new CommResp(BusinessDataCode.FAIL.getCode(), message);
	}

	/**
	 * 没有数据
	 *
	 * @return
	 */
	protected CommResp noData() {
		return CommResp.no_data_resp;
	}

	/**
	 * 参数不合法
	 *
	 * @return
	 */
	protected CommResp illegal() {
		return CommResp.args_error_resp;
	}


	/**
	 * 服务器异常
	 *
	 * @return
	 */
	protected CommResp serverError() {
		return CommResp.service_exception_resp;
	}

	/**
	 * 自定义
	 *
	 * @param businessDataCode
	 * @return
	 */
	protected CommResp myResp(BusinessDataCode businessDataCode) {
		return new CommResp(businessDataCode);
	}

	/**
	 * 自定义
	 *
	 * @param businessDataCode
	 * @param data
	 * @return
	 */
	protected CommResp myResp(BusinessDataCode businessDataCode, Object data) {
		CommResp resp = new CommResp(businessDataCode);
		resp.setData(data);
		return resp;
	}

	/**
	 * 自定义
	 *
	 * @param code
	 * @param message
	 * @return
	 */
	protected CommResp myResp(String code, String message) {
		return new CommResp(code, message);
	}

	/**
	 * 自定义
	 *
	 * @param code
	 * @param message
	 * @param data
	 * @return
	 */
	protected CommResp myResp(String code, String message, Object data) {
		CommResp resp = new CommResp(code, message);
		resp.setData(data);
		return resp;
	}

	protected String formatTime(Date date) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String createTime = dateFormat.format(date);
		if (null != createTime) {
			int time = (int) date.getTime();
			int day = time / 24 * 3600;
			if (day > 1) {
				createTime = createTime.substring(0, 10);
			} else if (day == 1) {
				createTime = "昨天";
			} else {
				createTime = createTime.substring(10, 16);
			}

		}
		return createTime;
	}

}
